﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoTaskSchedule.Core.Enums;
using AutoTaskSchedule.Core.Infrastructure;
using AutoTaskSchedule.Core.Models;
using AutoTaskSchedule.Core.Reflects;
using FluentScheduler;

namespace AutoTaskSchedule.Core.Business
{
    public class TaskFactory : ITaskFactory
    {
        protected Task _task;

        protected TaskFactory(Task task)
        {
            _task = task;
        }

        /// <summary>
        /// 获取程序集中的调度任务
        /// </summary>
        /// <returns></returns>
        public List<Infrastructure.IAutoTaskSchedule> GetAutoTaskSchedules()
        {
            return ReflectFactory.Reflect(_task.TaskAssemblyFullName);
        }

        /// <summary>
        /// 获取当前配置调度任务
        /// </summary>
        /// <returns></returns>
        public IAutoTaskSchedule GetAutoTaskSchedule()
        {
            var autoTaskSchedules = GetAutoTaskSchedules();
            foreach (var autoTaskSchedule in autoTaskSchedules)
            {
                if (autoTaskSchedule.GetType().Name == _task.ClassName)
                {
                    return autoTaskSchedule;
                }
            }
            return null;
        }

        public virtual void Create()
        {
            throw new NotImplementedException("请在子类中实现本方法");
        }
    }
}